package gov.pbc.nn.kjgl.AbroadApproval.ureport;

import gov.pbc.nn.core.dao.AbstractJdbcDao;
import gov.pbc.nn.core.utils.FileUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;




import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils;

import com.bstek.ureport.datasource.ReportDataProvider;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;



@Component
public class A2ReportFGBWDataProvider extends AbstractJdbcDao implements ReportDataProvider<A2Image> {

	@Override
	public List<A2Image> provide(String datasourceName, Map<String, Object> parameters) {
		String id = parameters.get("id").toString();
		if (id != null) {
			return getComputerRoomApplyEntry(id);
		}
		return null;
	}

	public List<A2Image> getComputerRoomApplyEntry(String id) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("id", id);
		String Sql = "select OPINION_IMG_ from nnpbc.t_uflo_opinion A where A.BUS_KEY_ = ? AND A.TASK_NAME_ like '%分管保卫部门行领导意见%' order by A.CREATE_DATE_ desc limit 1";
		return this.getJdbcTemplate().query(Sql, new Object[] { id }, new RowMapper<A2Image>() {
			@Override
			public A2Image mapRow(ResultSet rs, int rowNum) throws SQLException {
				A2Image crap = new A2Image();
				try {
					crap.setImagepath(returnImagePath(rs.getBytes("OPINION_IMG_")));
				} catch (IOException e) {
					e.printStackTrace();
				}
				return crap;
			}
		});
	}
	
    

	public String returnImagePath(byte[] b) throws IOException {
		if(b == null){
			return null;
		}
		else
		{
			UUID uuid = UUID.randomUUID();
			String basePath = "c:/temp/FGBW/";
			String attrPath = uuid + ".jpg";
			String path = basePath + attrPath;

			String tPath = "temp.jpg";
			
			try {
				File f = new File(basePath);
				if (f.exists()) {
					FileUtils.deleteDirectory(f);
				}
				if (!f.exists()) {
					f.mkdirs();
				}
				
				File tFile = new File(basePath, tPath);
				if (!tFile.exists()) {
					tFile.createNewFile();
				}
				
				File file = new File(basePath, attrPath);
				if (!file.exists()) {
					file.createNewFile();
				}
				FileOutputStream tempOutput = new FileOutputStream(tFile);
				FileOutputStream imageOutput = new FileOutputStream(file);
				
				String tempb = new String(b);
				String substringAfter = StringUtils.substringAfter(tempb, "data:image/jpeg;base64,");
				byte[] decodeBase64 = Base64.decodeBase64(substringAfter);
				FileCopyUtils.copy(decodeBase64, tempOutput);
				
	            InputStream is = new FileInputStream(tFile);
	            //OutputStream os = new FileOutputStream(new File("download/qrcode_test_1.jpg"));
				
	            A2ResizeImage.resizeImage(is, (OutputStream)imageOutput, 120, "jpg");
				// imageOutput.write(b, 0, b.length);
				// imageOutput.close();
				
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			return "file:" + path;
		}

	}
}
